<div class="ibox float-e-margins">
    <div class="ibox-title" style="border: none;">
        <h5 style="color: #00afef"><i>Add</i></h5>
        <div id="form-add-btn" class="ibox-tools">
            <button class="btn btn-sm btn-primary save-btn-tab" id="saveflex">Save</button>
            <button class="btn btn-sm btn-primary btn-outline close-add cancel-btn-tab">Cancel</button>
            <button style="display:  none;" class="btn btn-sm btn-primary btn-outline complete-btn-tab">Complete</button>
        </div>
    </div>
    <div class="ibox-content">
        <form class="form-horizontal" id="flexform">
            <div class="form-group">
                <label class="col-lg-4 control-label">Table Name</label>
                <div class="col-lg-5">
                    <select id="table_names" name="table_name" class="form-control chosen" required>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Field Name</label>
                <div class="col-lg-5">
                    <select id="table_col" name="table_col" class="form-control chosen" required>
                      <option value=""></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-4 control-label">Separator</label>
                <div class="col-lg-5">
                    <input type="text" name="separator" class="form-control">
                </div>
            </div>
        </form>
    </div>
    <div id="flexseq" style="display: none;">
      <button class="btn btn-primary btn-md" data-toggle="modal" data-target="#addNewa">Add</button>
      <div class="ibox-content">
        <table class="table dataTables table-bordered">
            <thead>
                <tr>
                    <th style="width: 80px">Seqment Seq.</th>
                    <th>Segment Length</th>
                    <th>Seqment Name</th>
                    <th>Source Type</th>
                    <th>Source Name</th>
                    <th>Default Value</th>
                    <th>Editable Flag</th>
                </tr>
              <tbody id="flexsegtable">
                  
              </tbody>
        </table>
      </div>
    </div>
</div>
<div class="modal fade" id="addNewa" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
           <h4 class="modal-title" id="myModalLabel">Flex Seq Seqment</h4>
        </div>
      <div class="modal-body">
        <div class="row">
          <form id="flexform_seqments" method="post" class="form-horizontal">
              <input type="hidden" id="flex_seq_id" name="flex_seq_id">
              <input type="hidden" name="LookUpType" value="">
              <div class="form-group">
                  <label class="col-lg-4 control-label">Seqment Seq</label>
                  <div class="col-lg-6">
                      <input type="text" name="segment_seq" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-4 control-label">Seqment Length</label>
                  <div class="col-lg-6">
                      <input type="text" name="segment_length" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-4 control-label">Seqment Name</label>
                  <div class="col-lg-6">
                      <input type="text" name="segment_name" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-4 control-label">Source Type</label>
                  <div class="col-lg-6">
                      <select id="source_type" name="source_type" class="form-control chosen" required>
                      </select>
                  </div>
              </div>
              <div class="form-group" id="source_name_f">
                  <label class="col-lg-4 control-label">Source Name</label>
                  <div class="col-lg-6">
                      <select id="source_name" name="source_name" class="form-control chosen" required>
                      </select>
                  </div>
              </div>
              <div class="form-group" id="default_value_f">
                  <label class="col-lg-4 control-label">Default Value</label>
                  <div class="col-lg-6">
                      <input type="text" name="default_values" class="form-control" value="">
                  </div>
              </div>
              <div id="formula_f" style="display: none;" class="form-group">
                <label class="col-lg-4 control-label"><i class="fa fa-info-circle"></i></label>
                <div class="col-lg-6">
                  <textarea name="" id="" cols="30" rows="10" class="form-control"></textarea>
                </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-4 control-label">Editable Flag</label>
                  <div class="col-lg-6">
                      <input type="checkbox" value="Y" checked name="editable_flag" class="i-checks">
                  </div>
              </div>
          </form>
        </div>
      </div>
      <div class="modal-footer">
           <button id="save_flex_seqments" type="button" class="btn btn-primary" >Save</button>
      </div>
    </div>
  </div>
</div> <!-- end-of-modal -->
<script>
    $(document).ready(function(){
        $('#prog_parama').hide();
        $('.chosen').chosen({width: "100%"});
        $(".chosen-select").chosen({disable_search_threshold: 10,width: '100%'});
        //programlist();
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });
        $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax') . '/get_tables'); ?>",
              dataType: 'json',
              success: function(e) {
                 var tmp = '';
                 tmp += '<option value="">--</option>';
                 $.each(e, function(i, item){
                    tmp += '<option value="'+e[i].Tables_in_<?php echo $this->db->database; ?> +'">'+e[i].Tables_in_<?php echo $this->db->database; ?> +'</option>';
                 });
                 $('#flexform #table_names').html(tmp);
                 $('select').trigger('chosen:updated');
              },
              error: function() {
              },
              complete: function() {
              },
              beforeSend: function(){
              }
          });

        $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax') . '/get_lookup_values'); ?>",
              dataType: 'json',
              success: function(e) {
                 var tmp = '';
                 tmp += '<option value="">--</option>';
                 $.each(e, function(i, item){
                    tmp += '<option value="'+e[i].lookup_code +'">'+e[i].meaning +'</option>';
                 });
                 $('#flexform_seqments #source_type').html(tmp);
                 $('select').trigger('chosen:updated');
              },
              error: function() {
              },
              complete: function() {
              },
              beforeSend: function(){
              }

            });

        $(document).on('change','#flexform #table_names',function(){
             $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/get_table_col'); ?>",
                dataType: 'json',
                data: {"table_name" : $(this).val() },
                success: function(e) {
                   var tmp = '';
                   tmp += '<option value="">--</option>';
                   $.each(e, function(i, item){
                      tmp += '<option value="'+e[i].Field +'">'+e[i].Field +'</option>';
                   });
                   $('#flexform #table_col').html(tmp);
                   $('select').trigger('chosen:updated');
                },
                error: function() {
                },
                complete: function() {
                },
                beforeSend: function(){
                }
              });
          });

        $(document).on('change','#flexform_seqments #source_type',function(){
          //alert();
          switch ($(this).val()) {
            case 'FIELD':
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/get_field_name'); ?>",
                dataType: 'json',
                data: {"id" : $('#flex_seq_id').val()},
                success: function(data){
                  var tmp = '';
                  $.each(data, function(i, item) {
                    tmp += "<option value='"+data[i].field_name+"'>"+ data[i].field_name +"</option>";
                  });
                  $('#flexform_seqments #source_name').html(tmp);
                   $('select').trigger('chosen:updated');
                   $('#source_name_f').show();
                },
                beforeSend: function(){
                }
              });
              $('#default_value_f').hide();
              $('#formula_f').hide();
              break;
            case 'MANUAL':
              $('#default_value_f').show();
              $('#source_name_f').hide();
              $('#formula_f').hide();
              break;
            case 'SEQUENCE':
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/get_app_seq'); ?>",
                dataType: 'json',
                success: function(data){
                  var tmp = '';
                  $.each(data, function(i, item) {
                    tmp += "<option value='"+data[i].seq_name+"'>"+ data[i].seq_name +"</option>";
                  });
                  $('#flexform_seqments #source_name').html(tmp);
                   $('select').trigger('chosen:updated');
                   $('#source_name_f').show();
                },
                beforeSend: function(){
                }
              });
              $('#default_value_f').hide();
              $('#formula_f').hide();
              break;
            case 'FORMULA':
              $('#default_value_f').show();
              $('#source_name_f').hide();
              break;
            default:
              alert('BLANK');
              break;
          }
           /*$.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax') . '/get_table_col'); ?>",
              dataType: 'json',
              data: {"table_name" : $(this).val() },
              success: function(e) {
                 var tmp = '';
                 tmp += '<option value="">--</option>';
                 $.each(e, function(i, item){
                    tmp += '<option value="'+e[i].Field +'">'+e[i].Field +'</option>';
                 });
                 $('#flexform #table_col').html(tmp);
                 $('select').trigger('chosen:updated');
              },
              error: function() {
              },
              complete: function() {
              },
              beforeSend: function(){
              }
            });*/
        });

        $("#save_flex_seqments").click(function(event) {
          if($("#flexform_seqments").valid() == true){
            $.ajax({
                  type: "POST",
                  url: "<?php echo base_url($this->session->userdata('forajax') . '/insertseg'); ?>",
                  dataType: 'json',
                  data: $("#flexform_seqments").serialize(),
                  success: function(data) {
                    if(data[0].err == 0){
                      notifymsg('success','Success!');
                      getsegments();
                       $('#addNewa').modal('hide');
                    }else{
                      notifymsg('error',data[0].msg);
                    }
                  },
                  error: function() {
                  },
                  complete: function() {
                  },
                  beforeSend: function(){
                  }
                });
          }
        });

        function getsegments(){
          $.ajax({
                  type: "POST",
                  url: "<?php echo base_url($this->session->userdata('forajax') . '/get_segments'); ?>",
                  dataType: 'json',
                  data: {'id' : $('#flex_seq_id').val() },
                  success: function(data) {
                      var tmp = '';
                      $.each(data, function(i, item) {
                         tmp += '<tr>'
                             +  '<td>' + data[i].segment_seq + '</td>'
                             +  '<td>' + data[i].segment_length + '</td>'
                             +  '<td>' + data[i].segment_name + '</td>'
                             +  '<td>' + data[i].source_type + '</td>'
                             +  '<td>' + data[i].source_name + '</td>'
                             +  '<td>' + data[i].default_value + '</td>'
                             +  '<td>' + data[i].editable_flag + '</td>'
                             +  '</tr>';
                      });

                      $('#flexsegtable').html(tmp);
                  },
                  error: function() {
                  },
                  complete: function() {
                  },
                  beforeSend: function(){
                  }
                });
        }

        $('#saveflex').click(function(){
             $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/insert'); ?>",
                dataType: 'json',
                data: $("#flexform").serialize(),
                success: function(data) {
                  if(data[0].err == 0){
                    notifymsg('success','Success!');
                    $('#flexseq').show();
                    $("#flexform").find(':input:not(:disabled)').prop('disabled',true);
                    $("select").trigger('chosen:updated');
                    $(".complete-btn-tab").show();
                    $(".cancel-btn-tab").hide();
                    $("#saveflex").hide();
                    $("#flex_seq_id").val(data[0].id);
                  }else{
                    notifymsg('error',data[0].msg);
                  }
                },
                error: function() {
                },
                complete: function() {
                },
                beforeSend: function(){
                }
              });

          });
        
        $("#flexform_seqments").validate({
            rules: {
                segment_seq: {
                   required: true,
                   number: true
                },
                segment_length: {
                   required: true,
                   number: true
                },
                segment_name: {
                   required: true
                },
                source_type: {
                   required: true
                },
                source_name: {
                   required: true
                },
                default_values: {
                   required: true
                }
            }
        });

        $("#prog_parama").validate({
            rules: {
                seq_no: {
                   required: true
                },
                name: {
                   required : true
                },
                desc: {
                   required: true
                },
                data_type: {
                   required: true
                }
            }
        });

        $('#cancel_add').click(function () {
                swal({
                  title: "Are you sure?",
                  text: "Your work will not be save",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonColor: "#DD6B55",
                  confirmButtonText: "Leave",
                  cancelButtonText: "Stay",
                  closeOnConfirm: true,
                  closeOnCancel: true },
              function (isConfirm) {
                  if (isConfirm) {
                    swal("success");
                      close_add(); active_search();
                      notifymsg("hide","hide");
                  } else {
                      swal("Cancelled");
                  }
              });
            });
        $('#addX, .complete-btn-tab').click(function(){
            close_add();
       });
    });
</script>